package mediaplayermanager;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class MPlayerManagerConfig 
{
	public static final String GVIA_COMMON_CONFIG_FILE = "/etc/gvia/gviaConfig.properties";
	private static final String MPLAYER_MANAGER_CONFIG_FILE = "mediaplayermanagerConfig.properties";
	protected static MPlayerManagerConfig singleton = null;
	private Properties gviaCommonProps;
	private Properties configProperties;
	private String jdbcConnectionURI;
	private String user;
	private String password;
	private String mplayerCommand;
	private int mplayerPauseSecs;
	private int mediaPlayerStartDelaySecs;

	private MPlayerManagerConfig() throws Exception
	{
		InputStream is = null;
		InputStream gviaIs = null;
		try
		{
			configProperties = new Properties();
			is = this.getClass().getResourceAsStream(MPLAYER_MANAGER_CONFIG_FILE);
			if (is == null)
				throw new Exception(MPLAYER_MANAGER_CONFIG_FILE + " file not found");
			configProperties.load(is);
			is.close();
			gviaCommonProps = new Properties();
			gviaIs = new FileInputStream(GVIA_COMMON_CONFIG_FILE);
			gviaCommonProps.load(gviaIs);
			gviaIs.close();
			setPassword();
			setUser();
			setJdbcConnectionURI();
			setMplayerCommand();
			setMplayerPauseSecs();
			setMediaPlayerStartDelaySecs();
		}
		catch(Exception e)
		{
			if (is != null)
				is.close();
			if (gviaIs != null)
				gviaIs.close();
			throw e;
		}
	}
	
	public static MPlayerManagerConfig getConfig() throws Exception 
	{
		try
		{
			if (singleton == null)
			{
				singleton = new MPlayerManagerConfig();
			}
		}
		catch(Exception e)
		{
			throw e;
		}
		return singleton;
	}
	
	public String getPassword() 
	{
		return password;
	}

	public void setPassword() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("password");
		if (propValue == null)
			throw new Exception("password not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			password = propValue;
	}

	public String getUser() 
	{
		return user;
	}

	public void setUser() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("user");
		if (propValue == null)
			throw new Exception("user not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			user = propValue;
	}

	private void setJdbcConnectionURI() throws Exception
	{
		String propValue = configProperties.getProperty("jdbcConnectionURI");
		if (propValue == null)
			throw new Exception("jdbcConnectionURI not added in " + MPLAYER_MANAGER_CONFIG_FILE + " file");
		else
			jdbcConnectionURI = propValue;
	}
	
	public String getJdbcConnectionURI()
	{
		return jdbcConnectionURI;
	}
	
	
	public String getMplayerCommand() 
	{
		return mplayerCommand;
	}

	public void setMplayerCommand() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("mplayerCommand");
		if (propValue == null)
			throw new Exception("mplayerCommand not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			mplayerCommand = propValue;
	}
	
	public int getMplayerPauseSecs() 
	{
		return mplayerPauseSecs;
	}
	
	public void setMplayerPauseSecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("mplayerPauseSecs");
		if (propValue == null)
			throw new Exception("mplayerPauseSecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			mplayerPauseSecs = Integer.parseInt(propValue);
	}

	public int getMediaPlayerStartDelaySecs() 
	{
		return mediaPlayerStartDelaySecs;
	}
	
	public void setMediaPlayerStartDelaySecs() throws Exception
	{
		String propValue = gviaCommonProps.getProperty("mediaPlayerStartDelaySecs");
		if (propValue == null)
			throw new Exception("mediaPlayerStartDelaySecs not added in " + GVIA_COMMON_CONFIG_FILE + " file");
		else
			mediaPlayerStartDelaySecs = Integer.parseInt(propValue);
	}
	

}
